Ext.ns('App');


App.WorkspaceFormPopupPanel = Ext.extend(Ext.Panel, {
    floating: true,
    modal: true,
    editable: true,
    scroll: false,
    layout: 'fit',
    fullscreen: true,
    
	/**
	 * Handles Save button click.
	 * @function doSave
	 */
    doSave: function(){
    	var panel = this;
		Ext.dispatch({
            controller: app.controllers.workspaceForm,
            action: 'save',
            form: this.getForm(),
            id: this.getForm().record ? this.getForm().record.getId() : null,
    		callback: {
				successFn: function (){
					panel.formContainer.hideMask();
				},
				failureFn: function (){
					panel.formContainer.hideMask();
				}					
			}
        });						
    	
    	/*
    	var panel = this;
    	if (this.dmlType === 'update')    	
			Ext.dispatch({
				controller: app.controllers.infoView,
				action: 'updateFAD',
				modifiedFields: this.getForm().getRecord().modifiedFields,
				infoDetail: this.infoDetail,
				formPanel: this,
				callback: {
					successFn: function (){
						panel.formContainer.hideMask();
					},
					failureFn: function (){
						panel.formContainer.hideMask();
					}					
				}
			});
    	else if (this.dmlType === 'insert')    	
			Ext.dispatch({
				controller: app.controllers.infoView,
				action: 'insertFAD',
				modifiedFields: this.getForm().getRecord().modifiedFields,
				fad: this.getFAD(),
				coordinates: this.getForm().getRecord().getCoordinatesString(),
				formPanel: this,
				callback: {
					successFn: function (){
						panel.formContainer.hideMask();
					},
					failureFn: function (){
						panel.formContainer.hideMask();
					}					
				}
				//modifiedFields: this.getForm().getRecord().modifiedFields,
				//infoDetail: this.infoDetail
			});
			*/
    },
    
	/**
	 * Clears all fields and prepare form for new workspace.
	 * @function prepareForNewRecord 
	 */    
	prepareForNewRecord: function(){
		this.formContainer.prepareForNewRecord();
	},

	/**
	 * Fills form with the record.
	 * 
	 * @param {NG.models.Workspace} record
	 */	
    prepareForRecord: function(record) {
        this.formContainer.load(record);

    },
    
    /**
     * @return {Ext.form.FormPanel} Form panel of the container.
     */
    getForm: function (){
    	return this.formContainer;
    },
    
    initComponent: function(){
    	var editable = this.editable;
        this.formContainer = new Ext.form.FormPanel({
            scroll: true,
            waitTpl: new Ext.XTemplate(WAIT_TPL),
            items: [{
                xtype: 'button',
                cls: 'apply-btn',
                ui: 'confirm-small',
                text: L_SAVE,
                disabled: !editable,
                width: 100,
                handler: function(){
                	this.formContainer.showMask({        
                		message : L_PLEASE_WAIT,
                        transparent : false,
                        target  : this.formContainer,  //optional target Element
                        cls : 'form-mask',
                        customImageUrl : 'app/resources/loading.gif'
                	});
                	this.doSave();
                    //this.hide();
                },
                scope: this 
            },{
                xtype: 'button',
                cls: 'close-btn',
                ui: 'decline-small',
                text: L_CLOSE,
                width: 100,	
                handler: function(){
                	this.destroy();
                }
                ,scope: this 
            }, {
                xtype: 'fieldset',
                scroll: false,
                title: L_FILL_WS_INFO,
                defaults: {
                	required: true,
                	listeners: {
                		change: function (comp, newValue, oldValue){
                			
                		}
                	}                                                	
                },
                items: [{
                    name : 'name',
                    label: L_WS_NAME,
                    xtype: 'textfield'
                }, {
                    name : 'host',
                    label: 'Url',
            		xtype: 'urlfield',
            		id: 'ng-ws-host'
                }, {
                    name : 'username',
                    label: L_USERNAME,
                    xtype: 'textfield'
                }, {
                    name : 'password',
                    label: L_PASSWORD,
                    xtype: 'passwordfield',
            		required: false
                },{
                    name : 'projectWholeWorld',
                    label: 'Sınırlar',
                    xtype: 'textfield',
            		disabled: true,
            		style: 'font-size: 11px;'
                },{
                	name: 'id',
                	hidden: true
                }]
            }],
        	/**
        	 * Clears all fields and prepare form for new workspace.
        	 */
        	prepareForNewRecord: function(){
        		this.reset();
        		var workspace = Ext.ModelMgr.create({
        		}, 'NG.models.Workspace');
        		workspace.setHost('http://');
        		this.load(workspace);
        		//this.items.items[0].focus();
        	},
        	
        	/**
        	 * Fills form with the record.
        	 * 
        	 * @param {NG.models.Workspace} record
        	 */
            prepareForRecord: function(record) {
                this.load(record);
                /*
                var toolbar = this.getDockedItems()[0];
                toolbar.getComponent('cancel').record = record;
                toolbar.getComponent('apply').record = record;
                toolbar.getComponent('apply').form = this;
                */
            }            
        });
        this.items = [{
            xtype: 'panel',
            layout: 'fit',
            items: [this.formContainer]
        }];
        App.WorkspaceFormPopupPanel.superclass.initComponent.call(this);
    }    

});

/**
 * @class NG.views.WorkspaceForm
 * @extends Ext.form.FormPanel
 * 
 * Used for register new workspace or update a registered workpspace.
 * 
 */
